var SEARCH_CUSTOMER_TYPE = 1; //用户组
var SEARCH_CUSTOMER_NAME = 2; //配送单位
var SEARCH_INTERNAL_TYPE = 4; //商品类型
var SEARCH_INTERNAL_NAME = 8; //商品名称
var SEARCH_DISTRIBUTION_DATE = 16; //配送时间
var SEARCH_CREATE_TIME_START = 32;//创建开始时间
var SEARCH_CREATE_TIME_END = 64;//创建结束时间
var SEARCH_STOREHOUSE_ID = 128; //库房ID
var SEARCH_STOREHOUSE_NAME = 256;//库房名称
var SEARCH_CREATE_MAN=512//制单人
var SEARCH_ORDER_NO =1024;//销售单编号
var SEARCH_DISTRIBUTION_DATE_START = 2048; //配送开始时间
var SEARCH_DISTRIBUTION_DATE_END = 4096; //配送结束时间
var SEARCH_DISTRIBUTION_DATE_START_REQUIRE = 8192;//配送开始时间必填
var SEARCH_DISTRIBUTION_DATE_END_REQUIRE = 16384;//配送开始时间必填
var SEARCH_STOREHOUSE_NAME_UNNECESSARY =32768;//库房名称不必须填写
var SEARCH_TRADE_TYPE=65536;
var SEARCH_CREATE_TIME_START_REQUIRE = 131072;
var SEARCH_CREATE_TIME_END_REQUIRE = 262144;
var searchDataList = [{
		"name": "用户组",
		"type": "CUSTOMER_TYPE",
		"className": "easyui-combobox",
		"required": false,
		"id": "search_customer_type"
	}, {
		"name": "配送单位",
		"type": "CUSTOMER_NAME",
		"className": "easyui-combobox",
		"id": "search_customr_name"
	},{
		"name": "商品类型",
		"type": "INTERNAL_TYPE",
		"className": "easyui-combobox",
		"required": false,
		"id": "search_internal_type"
	},
	{
		"name": "商品名称",
		"type": "INTERNAL_NAME",
		"className": "easyui-combobox",
		"required": false,
		"id": "search_commodity_name"
	},
	{
		"name": "配送时间",
		"type": "DISTRIBUTION_DATE",
		"className": "easyui-datebox",
		"required": false,
		"id": "search_distribution_date",
	},{
		"name": "建单开始时间",
		"type": "CREATE_TIME_START",
		"className": "easyui-datebox",
		"required": false,
		"id": "search_create_start",
	},{
		"name": "建单结束时间",
		"type": "CREATE_TIME_END",
		"className": "easyui-datebox",
		"required": false,
		"id": "search_create_end",
	},{
		"name": "库房名称",
		"type": "STOREHOUSE_ID",
		"className": "easyui-combobox",
		"required": true,
		"id": "search_storehouse_id",
	},{
		"name": "库房名称",
		"type": "STOREHOUSE_NAME", //库房
		"className": "easyui-combobox",
		"required": true,
		"id": "search_storehouse"
	},
	{
		"name": "制单人",
		"type": "CREATE_MAN",
		"className": "easyui-textbox",
		"required": false,
		"id": "search_create_man"
	},
	{
		"name":'销售单编号',
		"type":"ORDER_NO",
		"className": "easyui-textbox",
		"required": false,
		"id": "search_order_no"
	},
	{
		"name": "配送开始时间",
		"type": "DISTRIBUTION_DATE_START",
		"className": "easyui-datebox",
		"required": false,
		"id": "search_begin_time"
	}, {
		"name": "配送结束时间",
		"type": "DISTRIBUTION_DATE_END",
		"className": "easyui-datebox",
		"required": false,
		"id": "search_end_time"
	}, 
	{
		"name": "配送开始时间",
		"type": "DISTRIBUTION_DATE_START",
		"className": "easyui-datebox",
		"required": true,
		"id": "search_begin_time"
	}, 
	{
		"name": "配送结束时间",
		"type": "DISTRIBUTION_DATE_END",
		"className": "easyui-datebox",
		"required": true,
		"id": "search_end_time"
	},{
		"name": "库房名称",
		"type": "STOREHOUSE_NAME", //库房
		"className": "easyui-combobox",
		"required": false,
		"id": "search_storehouse"
	},
	{
		"name": "商品类型",
		"type": "TRADE_TYPE", //库房
		"className": "easyui-textbox",
		"required": false,
		"id": "search_trade_type"
	},{
		"name": "建单开始时间",
		"type": "CREATE_TIME_START",
		"className": "easyui-datebox",
		"required": true,
		"id": "search_create_start",
	},{
		"name": "建单结束时间",
		"type": "CREATE_TIME_END",
		"className": "easyui-datebox",
		"required": true,
		"id": "search_create_end",
	}
	
];

//取得查询条件的对象数组
function getSearchOptionList(searchType, newdata){
	var content = [];
	var s;
	var data = searchDataList;
	for(var i = 0; i < data.length; i++) {
		s = searchType & (1 << i);
		if(s != 0) {
			content.push(data[i]);
		}
	}
	if(newdata.length > 0) {
		for(var i = 0; i < newdata.length; i++) {
			content.push(newdata[i]);
		}
	}
	return content;
}

function constructSearchOptionFormat(type, newdata){
	var data = getSearchOptionList(type, newdata);
	var label = 0;
	var content = '';
	for(var i = 0, len = data.length; i < len; i++){
		if(label === 0){
			content += '<tr><td width="10%">' + data[i].name + '</td><td width="23%"><input class="' +
			   			data[i].className + '" style="width: 100%" type="text" name="' + data[i].type + 
			   			'" data-options="disabled:true" /></td>';
		}else if(label === 2){
			content += '<td width="10%">' + data[i].name + '</td><td width="23%"><input class="' +
			   			data[i].className + '" style="width: 100%" type="text" name="' + data[i].type + 
			   			'" data-options="disabled:true" /></td></tr>';
			label = -1;
		}else{
			content += '<td width="10%">' + data[i].name + '</td><td width="23%"><input class="' +
			   			data[i].className + '" style="width: 100%" type="text" name="' + data[i].type + 
			   			'" data-options="disabled:true" /></td>';
		}
		if(i === len -1 && label !== 2){
			content += '</tr>';
		}
		label ++;
	}
	content = '<form id="ff" action="#" method="post"><table cellpadding="2"  class="displaytable" style="text-align: left;width: 100%;margin: 0 auto;">' + 
				content + '</table></form>';
	return content;
}

function buildSearchOptions(searchType, newdata) {

	var content = "";
	var s;
	var data = searchDataList;
	for(var i = 0; i < data.length; i++) {
		s = searchType & (1 << i);
		if(s != 0) {
			content += "<tr><td style=\"width:25%\;text-align:left;\">" + data[i].name + "</td><td style=\"width:75%\"><input data-options=\"required:"+ data[i].required +"\" style=\"width: 80%\" class=\"" + data[i].className + "\" name=\"" + data[i].type + "\" id=\"" + data[i].id + "\" /></td></tr>"
		}
	}
	if(newdata.length > 0) {
		for(var i = 0; i < newdata.length; i++) {
			content += "<tr><td style=\"width:25%\;text-align:left;\">" + newdata[i].name + "</td><td style=\"width:75%\"><input style=\"width: 80%\" class=\"" + newdata[i].className + "\" name=\"" + newdata[i].type + "\" id=\"" + newdata[i].id + "\" /></td></tr>"
		}
	}
	var contain = "<table class=\"displaytable\">" + content + "</table>";
	contain += "<div style=\"text-align: center; margin-top:20px;\"><a href=\"#\" class=\"easyui-linkbutton\" onclick=\"doSearch()\" style=\"width:80px;\">查询</a>"
	contain += "<a href=\"#\" class=\"easyui-linkbutton\" onclick=\"closeForm()\" style=\"width:80px;margin-left:15px;\">取消</a></div>"
	return contain;
}

function processSearchCondition(isConstruct) {
	if($("#fm") == undefined) {
		console.log("请检查ID为div_search是否存在");
		return "";
	}
	var searchCondition = "";
	var data = {};
	$("#fm input[name]").each(function(index) {
		if($(this).val().trim().length != 0) {
			data[$(this).attr("name")] = $(this).val().trim();
			if(searchCondition == "") {
				searchCondition = $(this).attr("name") + "=" + $(this).val().trim();
			} else {
				searchCondition += "&" + $(this).attr("name") + "=" + $(this).val().trim();
			}
		}
	});
	if(isConstruct){
		$('#ff').form('clear');
		$('#ff').form('load', data);
	}
	return searchCondition;
}
